
using System;
using System.Collections.Generic;
using System.Text;

namespace SysTables
{
    /*
     * Databaza obsahuje zoznam tabuliek a funkcie na pracu s nimi(vyhladavanie, vytvaranie,mazanie)
     * Na disk sa ukladaju iba ID vsetkych tabuliek priradenych v danej databaze. Ak chceme menit strukturu tabuliek 
     * musime vyuzit triedu SysTables
     */
        [Serializable]
        public class Database
        {
            public Database(int ID,String Name,Sysdatabases parent)
            {
                mID = ID;
                mName = Name;
                mTables = new List<Table>();
                mTablesID = new List<int>();
                mParent = parent;
            }

            private int mID;
            public int ID
            {
                get { return mID; }
                set { mID = value; }
            }


            public Table FindTable(String name)
            {
                int i;
                return FindTable(name, out i);
            }

            public Table FindTable(String name,out int  index)
            {
                for (int i = 0; i < mTables.Count; i++)
                {
                    if (mTables[i].TableName == name)
                    {
                        index = i;
                        return mTables[i];
                    }
                }
                index = -1;
                return null;
            }

            public Boolean Add(Table tab)
            {
                mTables.Add(tab);
                mTablesID.Add(tab.ID);
                return true;
            }

            public Table Drop(string Name)
            {
                int index;
                Table tab = FindTable(Name, out index);
                if (tab==null) return null;
                mTablesID.RemoveAt(index);
                mTables.RemoveAt(index);
                return tab;
            }

            public void Reload(Systables tables)
            {
                mTables = new List<Table>();
                for (int i = 0; i < mTablesID.Count; i++)
                {
                    mTables.Add(null);
                }

                for (int j = 0; j < mTablesID.Count; j++)
                {
                    mTables[j] = tables.FindTable(mTablesID[j]);
                }


                //for (int i = 0; i < tables.Tables.Count; i++)
                //{
                //    for (int j = 0; j < mTablesID.Count; j++)
                //    {
                //        if (tables.Tables[i].ID == mTablesID[j])
                //            mTables[j] = tables.Tables[i];
                //    }
                //}
            }

            [NonSerialized]
            private Sysdatabases mParent;
            public Sysdatabases Parent
            {
                get { return mParent; }
                set { mParent = value; }
            }


            private List<int> mTablesID;
            public List<int> TablesID
            {
                get { return mTablesID; }
            }


            [NonSerialized]
            private List<Table> mTables ;
            public List<Table> Tables
            {
                get { return mTables; }
            }



            private String mName;
            public String Name
            {
                get { return mName; }
                set { mName = value; }
            }
            
        }

}
